package com.java.code.socket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;

public class NettyServer {
    private int port;

    private NettyServer(int port) {
        this.port = port;
    }

    private void runServer() throws Exception {
        // EventLoopGroup是用来处理IO操作的多线程事件循环器
        // bossGroup 接收连接请求
        EventLoopGroup parentGroup = new NioEventLoopGroup();
        // workerGroup 处理已经收到的请求
        EventLoopGroup childGroup = new NioEventLoopGroup();
        try {
            // 服务启动器
            ServerBootstrap sb = new ServerBootstrap();
            sb.group(parentGroup, childGroup)
                    //配置 Channel
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            //解码器需要设置数据的最大长度，我这里设置成1024
                            //用于解决粘包拆包问题,可以识别/r/n只要client发送数据时带上/r/n就不会粘包和拆包了
                            //ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
                            // 注册handler
                            ch.pipeline().addLast(new ServerHandler());
                        }
                    })
                    // 设置服务端接受连接的队列长度，如果队列已满，客户端连接将被拒绝
                    .option(ChannelOption.SO_BACKLOG, 128)
                    // 心跳保活
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            // 绑定端口，启动服务器
            ChannelFuture f = sb.bind(port).sync();//.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
            // 一直等待直到服务器 socket 关闭
            f.channel().closeFuture().sync();
        } finally {
            // 将所有的event loop都关闭
            parentGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new NettyServer(9999).runServer();
        System.out.println("over");
    }

}
